//
//  ShouChangViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/3/1.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "ShouChangViewController.h"
#import "deLISTTableViewCell.h"
#import "TUWENTableViewCell.h"
@interface ShouChangViewController ()<UITableViewDataSource, UITableViewDelegate, TargetTableViewCellDelegate>

@property (nonatomic, retain) NSMutableArray *SpotArr;
@property (nonatomic, retain) UITableView *tableView; /**<  底层tableView */
@property (nonatomic, retain) NSMutableArray *tripArr;/**<  装trip的数组 */
@property (nonatomic, retain) NSMutableArray *deList;/**<  装图文的数组 */
@property (nonatomic, assign) CGRect rect;
@property (nonatomic, retain) NSMutableArray *recommendationArr;/**<  评论头像数组 */

@property (nonatomic, retain) NSString *urlString;/**<  关于收藏的网址 */

@end

@implementation ShouChangViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)dealloc{
    [_SpotArr release];
    [_tableView release];
    [_tripArr release];
    [_deList release];
    [_recommendationArr release];
    [_urlString release];
    [_model release];
    [_shouChangModel release];
    [_url release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"故事详情";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initNavigationItem];
    [self handleData];
    NSLog(@"%@", _url);
    
}

/** 创建navigationItem */
- (void)initNavigationItem{
    
    UIImage *image = [UIImage imageNamed:@"iconfont-jiantouzuo.png"];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.leftBarButtonItem = item;
    [item release];
    UIImage *image2 = [UIImage imageNamed:@"iconfont-heilongjiangtubiao12.png"];
    UIBarButtonItem *item2= [[UIBarButtonItem alloc] initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(item2Action:)];
    UIImage *image3 = [UIImage imageNamed:@"iconfont-fenxiang.png"];
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithImage:image3 style:UIBarButtonItemStylePlain target:self action:@selector(item3Action:)];
    self.navigationItem.rightBarButtonItems = @[item2, item3];
    [item2 release];
    [item3 release];
}
/** item方法 */
- (void)itemAction:(UIBarButtonItem *)item{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)item2Action:(UIBarButtonItem *)item{
    
    [self createAlert];
    
}
- (void)createAlert{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"你已经收藏" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
}
- (void)item3Action:(UIBarButtonItem *)item{
    NSLog(@"分享");
}
/** 创建uitableView */
- (void)initTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 100) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    //注册
    [_tableView registerNib:[UINib nibWithNibName:@"StoryAllTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"StoryAllTableViewCell"];
    //注册
    [_tableView registerClass:[deLISTTableViewCell class] forCellReuseIdentifier:@"deLISTTableViewCell"];
    //注册
    [_tableView registerClass:[TUWENTableViewCell class] forCellReuseIdentifier:@"TUWENTableViewCell"];
    
    [_tableView registerNib:[UINib nibWithNibName:@"TWLastTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TWLastTableViewCell"];
    
    [_tableView registerNib:[UINib nibWithNibName:@"TargetTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TargetTableViewCell"];
    
    [_tableView registerClass:[LikeTableViewCell class] forCellReuseIdentifier:@"LikeTableViewCell"];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    
}
/** 代理方法 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _deList.count + 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        Spot *spotModel = _SpotArr[0];
        Trip *tripModel = _tripArr[0];
        
        StoryAllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoryAllTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (spotModel.poi == nil) {
            cell.tripModel = tripModel;
            cell.backgroundColor = UIColorFromRGB(0xF0F0F0);
        }else{
            cell.tripModel = tripModel;
            cell.spotModel = spotModel;
            cell.backgroundColor = UIColorFromRGB(0xF0F0F0);
        }
        return cell;
    }else if (indexPath.row == 1 ){
        Spot *model = _SpotArr[0];
        deLISTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"deLISTTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        
        
        return cell;
    }else if (indexPath.row == _deList.count + 2){
        TWLastTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TWLastTableViewCell"];
        Trip *model = _tripArr[0];
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }else if (indexPath.row == _deList.count + 3){
        
        TargetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TargetTableViewCell"];
        Spot *model = _SpotArr[0];
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        return cell;
        
    }else if (indexPath.row == _deList.count + 4){
        //判断的话reload就不好使了
        if (_recommendationArr.count == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
            
            return cell;
        }else{
            Spot *model = _SpotArr[0];
            LikeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LikeTableViewCell"];
            cell.modelArr = _recommendationArr;
            cell.count = _recommendationArr.count;
            cell.model = model;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        
    }else{
        TUWENTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TUWENTableViewCell"];
        deList *model = _deList[indexPath.row - 2];
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 100;
    }else if ( indexPath.row == 1){
        
        UITableViewCell *cell = [self tableView:_tableView cellForRowAtIndexPath:indexPath];
        
        return cell.frame.size.height;
    }else if (indexPath.row == _deList.count + 2){
        return 100;
    }else if (indexPath.row == _deList.count + 3){
        return 151;
    }else if(indexPath.row == _deList.count + 4){
        return 200;
    }else{
        UITableViewCell *cell = [self tableView:_tableView cellForRowAtIndexPath:indexPath];
        
        return cell.frame.size.height;
        
    }
    
}
/** 完成代理协议 */
- (void)JumpTOweb:(NSString *)str{
    ExperienceDetailsViewController *ex = [[ExperienceDetailsViewController alloc ] init];
    ex.url = str;
    [self.navigationController pushViewController:ex animated:YES];
    [ex release];
    
}





/** 数据处理 */
- (void)handleData{
    
    // http://api.breadtrip.com/v2/new_trip/spot/?spot_id=2387619094
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=%@", _url];
    
    NSLog(@"地城旅游id：%@", _url);
    http://api.breadtrip.com/v2/new_trip/spot/?spot_id=2387094962
    [AFNetWorkingHandle GETRequestWithURLString:url completionWithSuccess:^(id result) {
        
        NSDictionary *data = result[@"data"];
        NSDictionary *spot = data[@"spot"];
        NSDictionary *trip = data[@"trip"];
        
        self.SpotArr = [NSMutableArray array];
        
        self.tripArr = [NSMutableArray array];
        
        Spot *spotModel = [[Spot alloc] initWithDic:spot];
        
        //装图文的数组
        NSArray *arr = spotModel.detail_list;
        self.deList = [deList ModelHandleWithArray:arr];
        
        
        [_SpotArr addObject:spotModel];
        
        NSLog(@"%@", _deList);
        
        Trip *tripModel = [[Trip alloc] initWithDic:trip];
        [_tripArr addObject:tripModel];
        
        //评论头像数据处理
        NSArray *arrCom = spot[@"recommendations"];
        self.recommendationArr = [Recommendation ModelHandleWithArray:arrCom];
        
        NSLog(@"%ld", (unsigned long)_recommendationArr.count);
        [spotModel release];
        [tripModel release];
        [self initTableView];
        [_tableView reloadData];
    } withFailure:^(id error) {
        NSLog(@"%@", error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
